package com.feicheng.iou.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.entity.ReservationBackview;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.mapper.ReservationBackviewMapper;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.ReservationBackviewService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@Service
@Slf4j
@Setter
public class ReservationBackviewServiceImpl extends ServiceImpl<ReservationBackviewMapper, ReservationBackview> implements ReservationBackviewService {


    @Autowired
    private ReservationBackviewMapper reservationBackviewMapper; //预约回访映射

//    /**
//     * @Author wuweinan
//     * @Description 预约到访时间设置
//     * @Date 2021-6-29 16:12
//     * @param reservationBackview
//     * @return ResultInfo
//     */
//    @Override
//    public ResultInfo insertBackviewReservationTime(ReservationBackview reservationBackview) {
//        log.info("ReservationBackviewServiceImpl的insertBackviewReservationTime方法进入");
//        reservationBackview.setCt(LocalDateTime.now());
//         //添加一条预约回访信息
//        Integer insertCount = reservationBackviewMapper.insert(reservationBackview);
//
//
//         //插入返回条数校验
//        if(insertCount != 1){
//            throw new SystemException(ReturnCode.COMMON_RESERVATION_BACKVIEW_WRONG_INSERT_COUNT);
//        }
//        log.info("ReservationBackviewServiceImpl的insertBackviewReservationTime方法返回");
//        return ResultInfo.success();
//
//    }

    /**
     * @Author wuweinan
     * @Description 更改预约回访时间
     * @Date 2021-8-2 21:48
     * @param reservationBackview
     * @return ResultInfo
     */
    @Override
    public ResultInfo updateBackviewReservationTime(ReservationBackview reservationBackview) {
        //根据学生id修改预约回访的datetime字段,因为如此设计的话student表和reservationBackview表就是一对一了
        UpdateWrapper<ReservationBackview> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("datetime", reservationBackview.getDatetime()).set("uu", reservationBackview.getUu()).
                set("autor", reservationBackview.getAutor()).
                eq("id", reservationBackview.getId()).
                eq("student_id", reservationBackview.getStudentId());
        int update = reservationBackviewMapper.update(null, updateWrapper);
        //更新条数检验
        if(update != 1){
            throw new SystemException(ReturnCode.UPDATE_RESERVATION_BACKVIEW_FAIL);
        }
        return ResultInfo.success();
    }
}
